﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Microsoft.Extensions.Caching.Memory;

namespace JshRCSWebApi.Util.Cache
{
    public static class CacheFn
    {
        #region cache读/写
        private static MemoryCache cache = new MemoryCache(new MemoryCacheOptions());
        /// <summary>
        /// 保存cache值[24小时]
        /// </summary>
        /// <param name="ls_key">key</param>
        /// <param name="ls_value">值</param>
        public static void fn_save_cache(string ls_key, object ls_value)
        {
            int li_hours = 24;
            fn_save_cache(ls_key, li_hours, ls_value);
        }

        /// <summary>
        /// 保存cache值
        /// </summary>
        /// <param name="ls_key">key</param>
        /// <param name="hours">有效期</param>
        /// <param name="ls_value">值</param>
        public static void fn_save_cache(string ls_key, int hours, object ls_value)
        {
            cache.Set(ls_key, ls_value, new MemoryCacheEntryOptions
            {
                SlidingExpiration = TimeSpan.FromHours(hours)
            });
        }

        /// <summary>
        /// 保存cache值(传入分钟参数)
        /// </summary>
        /// <param name="ls_key">key</param>
        /// <param name="hours">有效期</param>
        /// <param name="ls_value">值</param>
        public static void fn_save_cacheminuts(string ls_key, int minuts, object ls_value)
        {
            cache.Set(ls_key, ls_value, new MemoryCacheEntryOptions
            {
                SlidingExpiration = TimeSpan.FromMinutes(minuts)
            });
        }

        /// <summary>
        /// 保存cache值(传入秒参数)
        /// </summary>
        /// <param name="ls_key">key</param>
        /// <param name="second">有效期</param>
        /// <param name="ls_value">值</param>
        public static void fn_save_cachesecond(string ls_key, int second, object ls_value)
        {
            cache.Set(ls_key, ls_value, new MemoryCacheEntryOptions
            {
                SlidingExpiration = TimeSpan.FromSeconds(second)
            });
        }

        /// <summary>
        /// 获取cache值 
        /// </summary>
        /// <param name="ls_key"></param>
        /// <returns></returns>
        public static object fn_get_cache(string ls_key)
        {
            try
            {
                object val = null;
                if (ls_key != null && ls_key != "" && cache.TryGetValue(ls_key, out val))
                {
                    return val;
                }
                else
                {
                    return null;
                }
            }
            catch { return null; }
        }

        /// <summary>
        /// 移除cache
        /// </summary>
        /// <param name="ls_key"></param>
        public static void fn_remove_cache(string ls_key)
        {
            if (!string.IsNullOrEmpty(ls_key))
            {
                cache.Remove(ls_key);
            }
        }

        /// <summary>
        /// 传入项目ID,移除所有缓存
        /// </summary>
        /// <param name="ls_pid">项目id</param>
        public static void fn_remove_cache_all(string ls_pid)
        {
            List<string> cachelist = GetCacheKeys();
            for (int i = 0; i < cachelist.Count; i++)
            {
                string ls_key = cachelist[i];
                if (ls_key.IndexOf("p" + ls_pid + "-") > -1)
                {
                    fn_remove_cache(ls_key);
                }
            }
        }
        /// <summary>
        /// 清理所有左键key
        /// </summary>
        /// <param name="ls_pid">项目id</param>
        public static void fn_remove_cache_leftkey(string ls_leftkey)
        {
            List<string> cachelist = GetCacheKeys();
            for (int i = 0; i < cachelist.Count; i++)
            {
                string ls_key = cachelist[i];
                if (ls_key.IndexOf("p" + ls_leftkey + "-") > -1)
                {
                    fn_remove_cache(ls_key);
                }
            }
        }

        /// <summary>
        /// 返回的呢缓存key
        /// </summary>
        /// <returns></returns>
        public static List<string> GetCacheKeys()
        {
            const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
            var entries = cache.GetType().GetField("_entries", flags).GetValue(cache);
            var cacheItems = entries as IDictionary;
            var keys = new List<string>();
            if (cacheItems == null) return keys;
            foreach (DictionaryEntry cacheItem in cacheItems)
            {
                keys.Add(cacheItem.Key.ToString());
            }
            return keys;
        }

        #endregion
    }
}
